package org.javaboy.demo.service;

import org.javaboy.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class UserService {

    @Autowired
    UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * 在方法上添加 @Transactional 注解表示开启了事务
     *
     * @Transactional 注解也可以放在类上面，放在类上面就表示类中所有的方法上都有事务
     *
     * @param from
     * @param to
     * @param money
     */
    @Transactional
    public void transferMoney(String from, String to, Double money) {
        userDao.minusMoney(from, money);
//        int i = 1 / 0;
        userDao.addMoney(to, money);
    }
}
